package mobi.greenplate.theweb.resource;

import java.net.URI;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import mobi.greenplate.theweb.converter.FriendConverter;
import mobi.greenplate.theweb.converter.FriendListConverter;
import mobi.greenplate.theweb.domain.Friend;
import mobi.greenplate.theweb.repository.FriendRepository;
import mobi.greenplate.theweb.user.UserUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;


@Path("/friend")
@Component
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class AdminFriendResource {

    @Inject
    private FriendRepository friendRepository;
    @Context
    private UriInfo uriInfo;

    @GET
    @Produces({"application/xml", "application/json"})
    public FriendListConverter getFriends(@QueryParam("first") @DefaultValue("0") int first,
            @QueryParam("max") @DefaultValue("10") int max) {
        final List<Friend> friendList = friendRepository.findAll(first, max);
        final FriendListConverter friendListConverter = new FriendListConverter(friendList);
        return friendListConverter;
    }

    @GET
    @Produces({"application/xml", "application/json"})
    @Path("{id}")
    public FriendConverter getFriendById(@PathParam("id") Long id) {
        final Friend friend = friendRepository.findById(id);
        final FriendConverter friendConverter = new FriendConverter(friend);
        return friendConverter;
    }

    @POST
    @Consumes({"application/xml", "application/json"})
    public Response createFriend(FriendConverter friendConverter) {
        final Friend forSave = friendConverter.getEntity();
        forSave.setId(null); //makes sure it is allways new..
        final Friend friend = friendRepository.save(forSave,UserUtils.currentUser());
        final URI uri = uriInfo.getAbsolutePathBuilder().
                path(friend.getId().toString()).
                build();
        return Response.created(uri).build();
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    @Path("{id}")
    public FriendConverter updateOrCreateFriend(@PathParam("id") Long id, FriendConverter friendConverter) {
       //if the resource doesn't exist.. it would be created...with PUT.. could be added additional check ... 
        final Friend forSave = friendConverter.getEntity();
        forSave.setId(id); //makes sure it is exactly the one that has to be updated
        final Friend friend = friendRepository.save(forSave,UserUtils.currentUser());
        return new FriendConverter(friend);
    }

    @DELETE
    @Path("{id}")
    public void deleteFriend(@PathParam("id") Long id) {
       friendRepository.delete(id);
    }
}

